package com.LachesismStorm.lachesism.commonTools;

import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * @Author lachesism
 * @Date 2024-12-31 17:55
 **/
public class test {


    // Step 1: 获取 PHPSESSID Cookie
    @SneakyThrows
    public static String getCertCookie() {
        String urlString = "https://www.hrjnjd.com/zscx.html";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        // 设置请求头
        connection.setRequestProperty("Accept", "image/webp,image/avif,image/jxl,image/heic,image/heic-sequence,video/*;q=0.8,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5");
        connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
        connection.setRequestProperty("Accept-Language", "zh-CN,zh-Hans;q=0.9");
        connection.setRequestProperty("Referer", "https://www.hrjnjd.com/zscx.html");
        connection.setRequestProperty("Priority", "u=5, i");
        connection.setRequestProperty("Sec-Fetch-Dest", "image");
        connection.setRequestProperty("Sec-Fetch-Mode", "no-cors");
        connection.setRequestProperty("Sec-Fetch-Site", "same-origin");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");

        // 获取响应头中的 cookies
        Map<String, List<String>> headers = connection.getHeaderFields();
        List<String> cookies = headers.get("Set-Cookie");

        // 提取 PHPSESSID cookie
        for (String cookie : cookies) {
            if (cookie.contains("PHPSESSID")) {
                return cookie.split(";")[0].split("=")[1]; // 提取 PHPSESSID 值
            }
        }
        return null;
    }

    // Step 2: 使用获取的 Cookie 请求验证码图片
    @SneakyThrows
    public static BufferedImage getCaptchaImage(String cookie) {
        String urlString = "https://www.hrjnjd.com/channel/memberCode/";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        // 设置请求头，带上获取到的 Cookie
        connection.setRequestProperty("Accept", "image/webp,image/avif,image/jxl,image/heic,image/heic-sequence,video/*;q=0.8,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5");
        connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
        connection.setRequestProperty("Accept-Language", "zh-CN,zh-Hans;q=0.9");
        connection.setRequestProperty("Referer", "https://www.hrjnjd.com/zscx.html");
        connection.setRequestProperty("Priority", "u=5, i");
        connection.setRequestProperty("Sec-Fetch-Dest", "image");
        connection.setRequestProperty("Sec-Fetch-Mode", "no-cors");
        connection.setRequestProperty("Sec-Fetch-Site", "same-origin");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
        connection.setRequestProperty("Cookie", "PHPSESSID=" + cookie);

        // 获取验证码图片
        InputStream inputStream = connection.getInputStream();
        BufferedImage captchaImage = ImageIO.read(inputStream); // 将输入流转换为图片
        return captchaImage;
    }

    public static void main(String[] args) {
        try {
            // Step 1: 获取 Cookie
            String cookie = getCertCookie();
            if (cookie != null) {
                System.out.println("PHPSESSID: " + cookie);

                // Step 2: 使用 Cookie 获取验证码图片
                BufferedImage captchaImage = getCaptchaImage(cookie);

                // 打印图片信息（或者可以保存到文件，或者直接发送到前端）
                System.out.println("Captcha Image: " + captchaImage);

                // 这里可以将图片直接转为 byte[] 或 Base64 编码，发送给前端
            } else {
                System.out.println("无法获取到 PHPSESSID Cookie");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
